package LeetCode;

public class Rotatinglinkedlist61 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode p = head;
        int len = 1;
        while (p.next != null) {
            p = p.next;
            len++;
        }
        p.next = head;
        int cnt = len - k%len;
        if(cnt == len){
            return head;
        }
        while (cnt > 0) {
            p = p.next;
            cnt--;
        }
        ListNode res = p.next;
        p.next = null;

        return res;
    }
}
